<template>
    <div class="conversation-list">
        <h3>最近消息</h3>

        <!-- 消息列表 -->
        <div v-for="item in conversations" :key="item.id" class="conversation-item" @click="openChat(item)">
            <el-avatar :src="item.avatar" size="40" />
            <div class="info">
                <div class="name">{{ item.name }}</div>
                <div class="last-message">{{ item.lastMessageContent }}</div>
                <div class="time">{{ item.time }}</div>
            </div>
            <div class="unread-count" v-if="item.unread > 0">{{ item.unread }}</div>
        </div>
    </div>
</template>

<script>
import { getToken } from '@/utils/auth'
export default {
    data() {
        return {
            conversations: []
        };
    },
    mounted() {
        this.loadConversations();
    },
    methods: {
        loadConversations() {
            fetch('/api/conversations', {
                headers: {
                    'Authorization': "Bearer " + getToken()
                }
            })
                .then(res => res.json())
                .then(data => {
                    this.conversations = data;
                });
        },
        openChat(item) {
            // 跳转到聊天窗口，或打开侧边栏
            this.$message.info(`正在与 ${item.name} 聊天`);
            // 可跳转：this.$router.push(`/chat/${item.id}`);
        }
    }
};
</script>

<style scoped>
.conversation-list {
    padding: 20px;
}

.conversation-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid #eee;
    cursor: pointer;
    transition: background-color 0.2s;
}

.conversation-item:hover {
    background-color: #f5f7fa;
}

.info {
    flex: 1;
    margin-left: 12px;
}

.name {
    font-weight: 500;
    color: #333;
}

.last-message {
    font-size: 12px;
    color: #999;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.time {
    font-size: 10px;
    color: #ccc;
    margin-top: 2px;
}

.unread-count {
    background: #e64340;
    color: white;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    font-size: 12px;
    margin-right: 10px;
}
</style>